package com.lmk.controller;

import com.lmk.domain.Product;
import com.lmk.domain.Store;
import com.lmk.feign.StoreServices;
import com.lmk.service.ProductService;
import com.lmk.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private StoreServices storeService;

    // 查询所有
    @GetMapping("/getAll")
    public List<Product> getAll(){
        return productService.getAll();
    }

    // 查询单个
    @GetMapping("/getById/{id}")
    public Product getById(@PathVariable("id") Long id){
        return productService.getById(id);
    }

    // 添加或修改
    @PostMapping("/addorupdate")
    public AjaxResult addorupdate(@RequestBody Product product, Store store){
        try {
            if(product.getId()==null){
                AjaxResult result = productService.add(product);
                return result;

                //storeService.addorupdate(store);
            } else {
                productService.update(product);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


}
